package com.bigkoo.pickerviewdemo.slice;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.bigkoo.pickerviewdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class TestAbilityslice extends AbilitySlice {
    private TimePickerView pvTime;
    private StackLayout mFrameLayout;
    private Button btnShow;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer rootLayout = (ComponentContainer)
                LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_test_ability, null, false);
        super.setUIContent(rootLayout);
        btnShow = (Button) rootLayout.findComponentById(ResourceTable.Id_btn_show);
        mFrameLayout = (StackLayout) rootLayout.findComponentById(ResourceTable.Id_fragmen_fragment);

        initTimePicker();

        btnShow.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                pvTime.show(component);
            }
        });


    }

    private void initTimePicker() {
        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        startDate.set(2013, 0, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2019, 11, 28);
        //时间选择器

        pvTime = new TimePickerBuilder(getAbility(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, Component v) {
                Button btn = (Button) v;
                btn.setText(getTime(date));
            }
        }).setLayoutRes(ResourceTable.Layout_pickerview_custom_time, new CustomListener() {
            @Override
            public void customLayout(Component v) {
                final Text tvSubmit = (Text) v.findComponentById(ResourceTable.Id_tv_finish);
                Image ivCancel = (Image) v.findComponentById(ResourceTable.Id_iv_cancel);
                tvSubmit.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        pvTime.returnData();

                    }
                });

                ivCancel.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {

                    }
                });
            }
        })
                .setType(new boolean[]{true, true, true, false, false, false})
                .setLabel("", "", "", "", "", "") //设置空字符串以隐藏单位提示   hide label
                .setDividerColor(Color.DKGRAY.getValue())
                .setContentTextSize(20)
                .setDate(selectedDate)
                .setRangDate(startDate, selectedDate)
                .setDecorView(mFrameLayout)//非dialog模式下,设置ViewGroup, pickerView将会添加到这个ViewGroup中
                .setOutSideColor(0x00000000)
                .setOutSideCancelable(false)
                .build();

        pvTime.setKeyBackCancelable(false);//系统返回键监听屏蔽掉
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
